#include "ControlGlobal.h"

s32 saveSettleMessage(SettleMessage *pSeetle)
{
    s32 ret = 0;

    ret = sdkFileWrite(FILE_SETTLE_INFO, (u8 *) pSeetle, sizeof(SettleMessage));

    return ret == SDK_FILE_OK ? SDK_OK : SDK_ERR;
}

s32 readSettleMessage(SettleMessage *pSeetle)
{
    u8 *temp = 0;
    s32 ret = 0;
    u32 len = sizeof(SettleMessage);

    if (NULL == pSeetle)
    {
        return SDK_ERR;
    }
    TraceD("sizeof(SettleMessage) =%d \r\n", len);
    temp = sdkGetMem(len);
    memset(temp, 0, len);

    ret = sdkFileGetSize(FILE_SETTLE_INFO);

    if (ret > 0 && sdkFileRead(FILE_SETTLE_INFO, temp, 0, &len) == SDK_FILE_OK)
    {
        memcpy(pSeetle, temp, sizeof(SettleMessage));
        ret = SDK_OK;
    }
    else
    {
        ret = SDK_ERR;
    }

    sdkFreeMem(temp);

    return ret;
}

s32 executeSettle(bool checkTmsVersion, bool logOut, bool isAutoSettle)
{
    s32 ret = 0;

    if (isSupportScript())
    {
        if(platform_script_upload_get_trade_func() != NULL)
        {
            platform_script_upload_get_trade_func()();
        }
    }

    libTransShowTitle(TRANS_TYPE_SETTLE);

    do
    {
        ret = showTradeSummaryInfo(SUMMARY_TYPE_SETTING);

        if (!isAutoSettle && SDK_OK != ret)
        {
            break;
        }

        ret = platform_settle_get_settle_func()(isAutoSettle);
        if (SDK_OK != ret)
        {
            break;
        }

        if (logOut)
        {
            platform_settle_get_logout_func()();
        }

        if (checkTmsVersion)
        {
            platform_settle_get_tms_download_func()(true);
        }

        ret = SDK_OK;
    } while (false);

    return ret;
}